package com.gkey.sole.base.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gkey.base.cache.PathParameter;
import com.gkey.base.cache.UsualParameter;
import com.gkey.base.web.BaseController;
import com.gkey.core.web.util.RandomValidateCode;
import com.gkey.core.web.util.SessionUtil;
import com.gkey.sole.base.bean.CUser;
import com.gkey.sole.base.service.LoginService;
import com.gkey.sole.dict.bean.DPathParam;

/**
 * @Title: 登陆web.java
 * @Package com.gkey.sole.base.controller
 * @Description: TODO
 * @author GuoKey
 * @date 2016年8月10日 下午6:09:19
 * @version 1.0
 */

@Controller
@RequestMapping(value = "/login")
public class LoginController extends BaseController<CUser, Integer> {
	

	protected Logger log = Logger.getLogger(LoginController.class);
	
	@Resource
	protected LoginService loginService;
	

	
	/**
	 * @Description: 跳转到登陆页面(前缀页面选取 )
	 * @param req
	 * @param res
	 * @param model
	 * @return
	 * @throws IOException   
	 * String  
	 * @throws
	 * @author keyGuo
	 * @date 2016年8月11日 上午9:41:19
	 */
	@RequestMapping
	public String index(HttpServletRequest req, HttpServletResponse res, Model model) throws IOException {
		String isIE = req.getParameter("isIE");
    	String returl = req.getParameter("returl");
    	model.addAttribute("isIE", isIE);
    	model.addAttribute("returl", returl);
    	DPathParam param = PathParameter.AcquirePathModel(req.getRequestURL()+"");
    	req.getSession().setAttribute("params", param);
		return param.getWebprefixs() +"login";
	}
	
	
	/**
	 * @Description: 验证码 
	 * @param req
	 * @param res
	 * @param model
	 * @throws IOException   
	 * void  
	 * @throws
	 * @author keyGuo
	 * @date 2016年8月12日 上午11:15:52
	 */
	@RequestMapping(value = "/getImage")
    public void getImageCode(HttpServletRequest req, HttpServletResponse res, Model model) throws IOException {
    	res.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
    	res.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
    	res.setHeader("Cache-Control", "no-cache");
    	res.setDateHeader("Expire", 0);
        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            randomValidateCode.getRandcode(req, res);//输出图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
	
	/**
	 * @Description: 登陆验证方法
	 * @param request
	 * @return   
	 * String  
	 * @throws
	 * @author keyGuo
	 * @date 2016年8月12日 下午2:30:12
	 */
	@RequestMapping(value = "/check")
    @ResponseBody
    public String check(HttpServletRequest request){
		try {
			String username = request.getParameter("username");
	    	String password = request.getParameter("password");
	    	String imageCode = request.getParameter("imageCode").toUpperCase();;
	    	String sImageCode = (String) request.getSession().getAttribute("RANDOMVALIDATECODEKEY");
	    	CUser cuser = loginService.getCUser(username);
	    	if(cuser == null){
	    		return "userError";
	    	}else if(!cuser.getPasswd().equals(password)){
	    		return "passwordError";
	    	}else if(cuser.getState() == -10){
	    		return "statusError";
	    	}else if(cuser.getUsertype() != 1){
	    		return "typeError";
	    	}else if(!imageCode.equals(sImageCode)){
	    		return "imageCodeError";
	    	}   
	    	return "yes";
		} catch (Exception e) {
			return "adminError";
		}
    	
    }
	
	
	/**
	 * @Description: 成功登陆验证session保存.
	 * @param request
	 * @return   
	 * String  
	 * @throws
	 * @author keyGuo
	 * @date 2016年8月12日 上午11:16:03
	 */
	@RequestMapping(value = "/dologin")
    public String dologin(HttpServletRequest request){
		
    	String username = request.getParameter("username");
    	String password = request.getParameter("password");
    	String returl = request.getParameter("returl");
    	
    	CUser cuser = loginService.getCUser(username, password);
    	
    	log.debug("login:"+cuser.getLoginname()+"|"+cuser.getName());
    	
        SessionUtil.setSessionAttribute(request, UsualParameter.SESSION_USER, cuser);//login_user
        
        SessionUtil.setSessionAttribute(request, UsualParameter.SESSION_USERID, cuser.getId());//login_userid
        
        if (returl != null && !"".equals(returl)) {
        	log.debug("returl:"+returl);        	
			return "redirect:" + returl.replaceAll("\\|", "&");
		}
        
        
        
        String url = "redirect:/welcome";  
        
        return url;
    }
	
	/**
	 * 
	 * @Description: 退出登录,清空session
	 * @param req
	 * @param res
	 * @param model
	 * @return
	 * @throws IOException   
	 * String  
	 * @throws
	 * @author keyGuo
	 * @date 2016年8月17日 下午4:39:06
	 */
	@RequestMapping(value = "/logout")
    public String logout(HttpServletRequest req, HttpServletResponse res, Model model)throws IOException {
    	SessionUtil.clearSession(req);
    	return "redirect:/login";
    }
	
	

}
